<?php

namespace app\adminapi\export;

use app\services\product\product\StoreProductServices;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class BaseExport
{


    protected $spreadsheet;
    protected $sheet;

    protected $header = [];
    protected $data  = [];
    protected $mergeField = null; // 新增属性用于存储需要合并单元格的字段名
    public $tableName = 'result.xlsx';

    public function createData(array $where)
    {

        return [];
    }

    public function __construct()
    {
        $this->spreadsheet = new Spreadsheet();
        $this->sheet = $this->spreadsheet->getActiveSheet();
    }

    public function setHeaders(array $headers)
    {
        foreach ($headers as $column => $header) {
            $this->sheet->setCellValue($column . '1', $header);
        }
    }

//    public function setData(array $data)
//    {
//        $row = 2;
//        foreach ($data as $rowData) {
//            $col = 'A';
//            foreach ($rowData as $cellData) {
//                $this->sheet->setCellValue($col++ . $row, $cellData);
//            }
//            $row++;
//        }
//
//    }

    public function setData(array $data)
    {
        $row = 2;
        foreach ($data as $rowData) {
            // 获取需要合并的单元格数量
            $mergeCellCount = $this->getMergeCellCount($rowData);

            // 设置当前行的数据
            $col = 'A';
            foreach ($rowData as $key => $cellData) {
                $this->sheet->setCellValue($col . $row, $cellData);
                $col++;
            }

            // 如果需要合并单元格且存在合并字段，则进行合并
            if ($mergeCellCount > 1 && $this->mergeField !== null) {
                $this->mergeCells($this->mergeField, $row, $mergeCellCount);
            }

            $row += $mergeCellCount;
        }
    }

    protected function getMergeCellCount(array $rowData)
    {
        // 检查是否存在合并字段以及该字段的值是否为数组
        if ($this->mergeField !== null && isset($rowData[$this->mergeField]) && is_array($rowData[$this->mergeField])) {
            return count($rowData[$this->mergeField]);
        }
        return 1;
    }

    public function mergeCells($fieldName, $startRow, $count)
    {
        $startCol = array_search($fieldName, $this->header) + 1; // 获取字段所在的列

        // 计算合并单元格的范围
        $endRow = $startRow + $count - 1;
        $endCol = $startCol;

        // 合并单元格
        $this->sheet->mergeCellsByColumnAndRow($startCol, $startRow, $endCol, $endRow);
    }


    protected function export_end($filename)
    {
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($this->spreadsheet);
        $writer->save('php://output');
        exit;
    }

    public function export($where)
    {

        $columnRange = range('A', chr(65 + count($this->header) - 1));

        // Set headers and data
        $this->setHeaders(array_combine($columnRange, $this->header));
        $this->setData($this->createData($where));

        // Export the data
        $this->export_end($this->tableName);

    }
}